﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;

namespace Web.SetSystem
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            //判断用户权限
            if (!((Account)HttpContext.Current.Session["Account"]).RoleCurrent.Equals('a')) Response.Redirect("Default.aspx");

            if (!IsPostBack)
            {
                refreshGridview();
                fillDropdownlist();

                //绑定班级DropDownList
                ArrayList arraylist = new ArrayList();
                arraylist = Library.GetClassLib();
                if (arraylist.Count!=0)
                {
                    for (int i = 0; i < arraylist.Count; i++)
                    {
                        ListItem listitem = new ListItem();
                        listitem = (ListItem)arraylist[i];
                        DropDownList_ClassInfo1.Items.Add(listitem);
                    }
                    refreshstulist();

                }
                else DropDownList_ClassInfo1.Items.Add("空");
                refreshDFDropdownlist();
            }
        }


        protected void refreshDFDropdownlist()
        {
            //绑定答辩场次信息DropDownList
            ArrayList arraylistDef = new ArrayList();
            //获取该年所有答辩场次ID
            arraylistDef = Defense.GetDefenseListByYear(DeadLine.GetDeadLineYear());
            DropDownList_DefenseInfo1.Items.Clear();
            if (arraylistDef == null) DropDownList_DefenseInfo1.Items.Add("空");
            else
            {
                DropDownList_DefenseInfo1.Items.Clear();
                for (int i = 0; i < arraylistDef.Count; i++)
                {
                    ListItem myListItem = new ListItem();
                    myListItem.Value = arraylistDef[i].ToString();
                    myListItem.Text = Defense.GetDefenseName(decimal.Parse(myListItem.Value));
                    DropDownList_DefenseInfo1.Items.Add(myListItem);
                }
            }
           
        }

        protected void refreshstulist()
        {
            //所选班级的学生DataSet

            if (DropDownList_ClassInfo1.SelectedItem.Text != "空")
            {
                ClassInfo myClassInfo = new ClassInfo(DropDownList_ClassInfo1.SelectedItem.Text.ToString());
                DataSet ds = myClassInfo.GetStudentsDataSet();
                if (ds != null)
                {
                    //拼接的DataSet
                    DataSet ds2 = new DataSet();
                    DataTable dt = new DataTable();
                    ds2.Tables.Add(dt);
                    dt.Columns.Add("stdid");
                    dt.Columns.Add("stdname");
                    dt.Columns.Add("defenseinfo");


                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        //某学生的答辩信息DataSet
                        DataSet ds1 = new DataSet();
                        ds1 = Defense.GetDefenseList(ds.Tables[0].Rows[i][0].ToString());

                        DataRow dr = dt.NewRow();
                        dr["stdid"] = ds.Tables[0].Rows[i]["std_id"].ToString();
                        dr["stdname"] = ds.Tables[0].Rows[i]["std_name"].ToString();
                        if (ds1 != null) dr["defenseinfo"] = "时间：" + ds1.Tables[0].Rows[0]["def_time"].ToString() + "地点： " + ds1.Tables[0].Rows[0]["def_room"].ToString();
                        dt.Rows.Add(dr);

                    }

                    //绑定自定义DataSet
                    GridView_StuList.DataSource = ds2;
                    GridView_StuList.DataBind();
                }
            }
        }

        protected void refreshGridview()
        {
            ArrayList myAL = Defense.GetDefenseListByYear(DeadLine.GetDeadLineYear());
            ArrayList mydatasource = new ArrayList();
            if (myAL != null)
            {
                for (int i = 0; i < myAL.Count; i++)
                {
                    DefenseInfo myDI = Defense.GetDefenseInfo(decimal.Parse(myAL[i].ToString()));
                    if (myDI.DefenseTeacher1.Trim() != "空") myDI.DefenseTeacher1 = Staff.GetName(myDI.DefenseTeacher1);
                    if (myDI.DefenseTeacher2.Trim() != "空") myDI.DefenseTeacher2 = Staff.GetName(myDI.DefenseTeacher2);
                    if (myDI.DefenseTeacher3.Trim() != "空") myDI.DefenseTeacher3 = Staff.GetName(myDI.DefenseTeacher3);
                    if (myDI.DefenseTeacher4.Trim() != "空") myDI.DefenseTeacher4 = Staff.GetName(myDI.DefenseTeacher4);
                    if (myDI.DefenseTeacher5.Trim() != "空") myDI.DefenseTeacher5 = Staff.GetName(myDI.DefenseTeacher5);
                    if (myDI.DefenseAssistant.Trim() != "空") myDI.DefenseAssistant = Staff.GetName(myDI.DefenseAssistant);
                    mydatasource.Add(myDI);
                }
            }
                GridView_defenselib.DataSource = mydatasource;
                GridView_defenselib.DataBind();            
        }

        protected void fillDropdownlist()
        {
            DataSet myDs = Teacher.GetAllTeacher();
            ListItem LI = new ListItem("空", null);
            DropDownList_tch1.Items.Add(LI);
            DropDownList_tch2.Items.Add(LI);
            DropDownList_tch3.Items.Add(LI);
            DropDownList_tch4.Items.Add(LI);
            DropDownList_tch5.Items.Add(LI);
            DropDownList_tchassist.Items.Add(LI);
            for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
            {
                ListItem myLI = new ListItem(myDs.Tables[0].Rows[i]["tch_name"].ToString(),myDs.Tables[0].Rows[i]["tch_id"].ToString());
                DropDownList_tch1.Items.Add(myLI);
                DropDownList_tch2.Items.Add(myLI);
                DropDownList_tch3.Items.Add(myLI);
                DropDownList_tch4.Items.Add(myLI);
                DropDownList_tch5.Items.Add(myLI);
                DropDownList_tchassist.Items.Add(myLI);
            }
        }

        protected void ImageButton_addDFinfo_Click(object sender, ImageClickEventArgs e)
        {
            Defense myDefense = new Defense();
            DefenseInfo myDI = new DefenseInfo();
            Label_result.Text = string.Empty;
            try
            { myDI.DefenseTime = DateTime.Parse(TextBox_defensetime.Text); }
            catch { Label_result.Text = "不是有效的答辩日期时间"; }
            if (Label_result.Text != "不是有效的答辩日期时间")
            {
                myDI.DefenseRoom = TextBox_defensesite.Text;
                myDI.DefenseTeacher1 = DropDownList_tch1.SelectedItem.Value;
                myDI.DefenseTeacher2 = DropDownList_tch2.SelectedItem.Value;
                myDI.DefenseTeacher3 = DropDownList_tch3.SelectedItem.Value;
                myDI.DefenseTeacher4 = DropDownList_tch4.SelectedItem.Value;
                myDI.DefenseTeacher5 = DropDownList_tch5.SelectedItem.Value;
                myDI.DefenseAssistant = DropDownList_tchassist.SelectedItem.Value;
                Label_result.Text = myDefense.AddDefenseInfo(myDI);

                refreshGridview();
                refreshDFDropdownlist();
                TextBox_defensesite.Text = "";
                TextBox_defensetime.Text = "";
                DropDownList_tch1.Items.Clear();
                DropDownList_tch2.Items.Clear();
                DropDownList_tch3.Items.Clear();
                DropDownList_tch4.Items.Clear();
                DropDownList_tch5.Items.Clear();
                DropDownList_tchassist.Items.Clear();
                fillDropdownlist();
            }
        }

        protected void ImageButton_grvdel_Click(object sender, ImageClickEventArgs e)
        {
            Defense myDefense = new Defense();
            Label_result.Text = myDefense.DeleteDefenseInfo(decimal.Parse(((ImageButton)sender).CommandArgument.ToString()));

            refreshGridview();
            refreshDFDropdownlist();            
        }

        protected void GridView_defenselib_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_defenselib.PageIndex = e.NewPageIndex;
            refreshGridview();
        }

        protected void ImageButton_grvedit_Click(object sender, ImageClickEventArgs e)
        {
            DefenseInfo myDI = Defense.GetDefenseInfo(decimal.Parse(((ImageButton)sender).CommandArgument));
            TextBox_defensesite.Text = myDI.DefenseRoom;
            TextBox_defensetime.Text = myDI.DefenseTime.ToString();
            ListItem LI1, LI2, LI3, LI4, LI5, LI6;
            if (myDI.DefenseTeacher1.Trim()!="空") LI1 = new ListItem(Staff.GetName(myDI.DefenseTeacher1), myDI.DefenseTeacher1);
            else LI1 = new ListItem("空",null);
            if (myDI.DefenseTeacher2.Trim() != "空") LI2 = new ListItem(Staff.GetName(myDI.DefenseTeacher2), myDI.DefenseTeacher2);
            else LI2 = new ListItem("空", null);
            if (myDI.DefenseTeacher3.Trim() != "空") LI3 = new ListItem(Staff.GetName(myDI.DefenseTeacher3), myDI.DefenseTeacher3);
            else LI3 = new ListItem("空", null);
            if (myDI.DefenseTeacher4.Trim() != "空") LI4 = new ListItem(Staff.GetName(myDI.DefenseTeacher4), myDI.DefenseTeacher4);
            else LI4 = new ListItem("空", null);
            if (myDI.DefenseTeacher5.Trim() != "空") LI5 = new ListItem(Staff.GetName(myDI.DefenseTeacher5), myDI.DefenseTeacher5);
            else LI5 = new ListItem("空", null);
            if (myDI.DefenseAssistant.Trim() != "空") LI6 = new ListItem(Staff.GetName(myDI.DefenseAssistant), myDI.DefenseAssistant);
            else LI6 = new ListItem("空", null);
            DropDownList_tch1.SelectedIndex = DropDownList_tch1.Items.IndexOf(LI1);
            DropDownList_tch2.SelectedIndex = DropDownList_tch2.Items.IndexOf(LI2);
            DropDownList_tch3.SelectedIndex = DropDownList_tch3.Items.IndexOf(LI3);
            DropDownList_tch4.SelectedIndex = DropDownList_tch4.Items.IndexOf(LI4);
            DropDownList_tch5.SelectedIndex = DropDownList_tch5.Items.IndexOf(LI5);
            DropDownList_tchassist.SelectedIndex = DropDownList_tchassist.Items.IndexOf(LI6);

            ImageButton_addDFinfo.Visible = false;
            ImageButton_saveDFinfo.Visible = true;
            Label_result.Text = "";

            ImageButton_saveDFinfo.CommandArgument = ((ImageButton)sender).CommandArgument;
        }

        protected void ImageButton_saveDFinfo_Click(object sender, ImageClickEventArgs e)
        {
            Defense myDefense = new Defense();
            DefenseInfo myDI = new DefenseInfo();
            try
            {
                myDI.Id = decimal.Parse(((ImageButton)sender).CommandArgument);
                myDI.DefenseTime = DateTime.Parse(TextBox_defensetime.Text);
                myDI.DefenseRoom = TextBox_defensesite.Text;
                myDI.DefenseTeacher1 = DropDownList_tch1.SelectedItem.Value;
                myDI.DefenseTeacher2 = DropDownList_tch2.SelectedItem.Value;
                myDI.DefenseTeacher3 = DropDownList_tch3.SelectedItem.Value;
                myDI.DefenseTeacher4 = DropDownList_tch4.SelectedItem.Value;
                myDI.DefenseTeacher5 = DropDownList_tch5.SelectedItem.Value;
                myDI.DefenseAssistant = DropDownList_tchassist.SelectedItem.Value;
                Label_result.Text = myDefense.EditDefenseInfo(myDI);
                
            }
            catch (Exception ex)
            {
                Label_result.Text = ex.Message;
            }

            refreshGridview();
            refreshDFDropdownlist();
            ImageButton_addDFinfo.Visible = true;
            ImageButton_saveDFinfo.Visible = false;

            TextBox_defensesite.Text = "";
            TextBox_defensetime.Text = "";
            DropDownList_tch1.Items.Clear();
            DropDownList_tch2.Items.Clear();
            DropDownList_tch3.Items.Clear();
            DropDownList_tch4.Items.Clear();
            DropDownList_tch5.Items.Clear();
            DropDownList_tchassist.Items.Clear();
            fillDropdownlist();
        }

        protected void DropDownList_ClassInfo1_SelectedIndexChanged(object sender, EventArgs e)
        {
            refreshstulist();
        }
        //为CheckBox选中学生添加答辩场次信息
        protected void ImageButton_StuAdd_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i < GridView_StuList.Rows.Count - 1; i++)
            {
                CheckBox checkbox = (CheckBox)GridView_StuList.Rows[i].FindControl("CheckBox1");
                if (checkbox.Checked)
                {
                    Student myStudent = new Student(GridView_StuList.Rows[i].Cells[0].Text.ToString());
                    Label_StuDefenseResult.Text = myStudent.AddDefense(decimal.Parse(DropDownList_DefenseInfo1.SelectedItem.Value.ToString()));
                }
            }

            refreshstulist();
        }

        protected void GridView_StuList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_StuList.PageIndex = e.NewPageIndex;
            refreshstulist();
        }

        protected void ImageButton_StuDel_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i < GridView_StuList.Rows.Count - 1; i++)
            {
                CheckBox checkbox = (CheckBox)GridView_StuList.Rows[i].FindControl("CheckBox1");
                if (checkbox.Checked)
                {
                    Student myStudent = new Student(GridView_StuList.Rows[i].Cells[0].Text.ToString());
                    Label_StuDefenseResult.Text = myStudent.DeleteDefense();
                }
            }

            refreshstulist();
        }
    }
}
